<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <b-steps :current="stepVal" direction="vertical" @change.self="stepChange">
        <b-step title="基本信息">
          <a-card v-show="activeKey[0]" slot="description" v-loading="loading" :bordered="false">
            <a-form-model ref="ruleForm" :model="formData" :rules="rules">
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="物资招标定标编号">
                    <BaseInput v-model="formData.selectionCode" disabled />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="项目名称">
                    <BaseInput v-model="formData.projectName" disabled />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="物资招标评审会签编码" prop="tenderReviewCountersignId">
                    <!--                    <a-select-->
                    <!--                      :disabled="state === 3"-->
                    <!--                      v-model="formData.tenderReviewCountersignId"-->
                    <!--                      show-search-->
                    <!--                      placeholder="请选择"-->
                    <!--                      option-filter-prop="children"-->
                    <!--                      :filter-option="filterOption"-->
                    <!--                      @change="handleChange"-->
                    <!--                      :loading="selectLoading"-->
                    <!--                    >-->
                    <!--                      <a-select-option :value="item.id" v-for="item in optionArr">{{ item.countersignCode }}-->
                    <!--                      </a-select-option>-->
                    <!--                    </a-select>-->
                    <a-input-search
                      :disabled="state === 3"
                      v-model="formData.tenderCountersignCode"
                      @click="tenderReviewCountersignCodeVisible = true"
                      @search="tenderReviewCountersignCodeVisible = true"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="投标人数量">
                    <BaseInput v-model="formData.tenderSize" disabled />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="中标人数量">
                    <BaseInput v-model="formData.winningSize" disabled />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="是否集采">
                    <BaseInput v-model="formData.isCollect" disabled />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput v-model="formData.creatorName" disabled />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput v-model="formData.createTime" disabled />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div v-if="state !== 3" class="pub-next-btn">
              <a-button v-btn-permission="'mm_036_sav_05'" type="primary" @click="save(false)">
                保存草稿
              </a-button>
              <a-button type="primary" @click="save(true)">下一步</a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="招标定标明细">
          <a-card v-if="activeKey[1]" slot="description" :bordered="false">
            <materials
              :id="formData.id"
              ref="materials"
              :state="state"
              @update="update"
            />
          </a-card>
        </b-step>
      </b-steps>
      <div v-if="!isAudit" class="pub-header-btn page-btn-right-top">
        <template v-if="state !== 3">
          <a-button v-btn-permission="'mm_036_sub_06'" :loading="loading" type="primary" @click="submit">提交</a-button>
        </template>
        <a-button v-checkVisibleInDetail @click="$close">关闭</a-button>
      </div>
    </base-breadcrumb>
    <tenderReviewCountersignCode
      :visible.sync="tenderReviewCountersignCodeVisible"
      @ok="selectRow"
    />
  </div>
</template>

<script>
import materials from './components/materials'
import { createByMain, editByMain, queryById } from '@/api/material/procurement/tender'
import { signList } from '@/api/material/procurement/sign'
import tenderReviewCountersignCode from '@/views/materials/procurement/tender/components/tenderReviewCountersignCode'

export default {
  name: 'a' + Date.now(),
  components: {
    materials,
    tenderReviewCountersignCode
  },
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tenderReviewCountersignCodeVisible: false,
      activeKey: [true, false, false, false, false, false, false],
      state: Number(this.$route.query.state),
      isSubmit: false,
      loading: false,
      selectLoading: false,
      row: this.$route.query.row,
      formData: {
        createTime: this.getNowTime(),
        creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
        tenderReviewCountersignId: undefined
      },
      optionArr: [],
      stepVal: 0,
      isNext: false,
      rules: {
        tenderReviewCountersignId: { required: true, message: '请选择物资招标评审会签编码', trigger: 'change' }
      }
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.state = 3
        }
      }
    },
    stepVal(newVal, oldVal) {
      this.$set(this.activeKey, newVal, true)
    }
  },
  mounted() {
    this.getOption()
    if (this.state !== 1) {
      this.formData.id = this.$route.query.id
      this.getEditInfo()
      this.isSubmit = true
    }
    if (this.state === 3) {
      this.activeKey[1] = true
    }
  },
  methods: {
    selectRow(row) {
      this.formData.tenderCountersignCode = row.countersignCode
      this.formData.tenderReviewCountersignId = row.id
      this.handleChange(row.id)
    },
    async update() {
      let res = await queryById({ id: this.formData.id })
      if (res.code === 200) {
        this.formData = { ...this.formData, ...res.data }
      }
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange(value) {
      let res = this.optionArr.find(v => v.id === value)
      console.log('res',res)
      if (res) {
        if (res.projectIdByAuth) {
          this.formData.projectId = res.projectIdByAuth
          this.formData.isCollect = '否'
        } else {
          this.formData.isCollect = '是'
        }
        this.formData.projectName = res.projectNameByAuth
        this.formData.branchComName = res.branchCompanyName
        this.formData.branchComId = res.branchCompanyId
      }
    },
    previous() {
      this.isNext = true
      this.stepVal--
      if (this.stepVal < 0) {
        this.stepVal = 0
      }
    },
    next(val) {
      this.isNext = true
      this.stepVal++
    },
    stepChange(val) {
      if (!this.formData.id && !this.isAudit) {
        this.$message.warning('切换步骤前请先填写基本信息')
        this.stepVal = 0
      } else {
        if (!this.isNext) {
          this.$set(this.activeKey, val, !this.activeKey[val])
        }
      }
      this.isNext = false
    },
    async getOption() {
      this.selectLoading = true
      let res = await signList({ pageNo: 1, pageSize: 10000 })
      this.selectLoading = false
      if (res.code === this.SUCCESS_CODE) {
        this.optionArr = res.data
      }
    },
    // 获取编辑回显数据
    async getEditInfo() {
      this.loading = true
      let res = await queryById({ id: this.formData.id })
      if (res.code === 200) {
        this.formData = { ...this.formData, ...res.data }
      }
      this.loading = false
    },
    async submit() {
      if (this.formData.id) {
        if (await this.save(false, 'SUBMITTED')) {
          this.$close()
        }
      } else {
        if (await this.save(false, 'DRAFT')) {
          await this.submit()
        }
      }
    },
    async save(isNext, status = 'DRAFT') {
      return new Promise(async resolve => {
        this.$refs.ruleForm.validate(async valid => {
          if (valid) {
            let res
            this.loading = true
            if (status) {
              this.formData.auditStatus = status
            }
            if (this.formData.id) {
              res = await editByMain({}, this.formData)
            } else {
              res = await createByMain({}, this.formData)
            }
            this.loading = false
            if (res.code === this.SUCCESS_CODE) {
              if (!this.formData.id) {
                this.formData.id = res.data.id
                await this.getEditInfo()
              }
              if (status === 'DRAFT') {
                this.$message.success('保存成功')
              } else {
                this.$message.success('提交成功')
              }
              if (isNext) {
                this.next()
              }
              resolve(true)
            }
          } else {
            this.$noticeWarning('请填写完整')
          }
        })
      })
    }
  }
}
</script>
